from channels.layers import get_channel_layer
from asgiref.sync import async_to_sync
import random
from app01 import models, sergy

from django.http import JsonResponse
from rest_framework.views import APIView

class AdminDel(APIView):
    def post(self, request):
        models.Admin.objects.filter(id=request.data.get('id')).delete()
        return JsonResponse({"code": 200, "msg": "删除成功"})
class AdminRole(APIView):
    def get(self, request):
        adminRole = models.AdminRole.objects.all()
        adminRole_list = sergy.AdminRoleSerializer(adminRole, many=True)
        return JsonResponse({"code": 200, "msg": "查询成功", "data": adminRole_list.data})
    def post(self, request):
        role_list = request.data.get('role_list')
        adminid=request.data.get('adminid')
        for role in role_list:
            models.AdminRole.objects.create(admin_id=adminid,role_id=role)
        return JsonResponse({"code": 200, "msg": "添加成功"})
class AdminList(APIView):
    def get(self, request):
        adminrole = models.AdminRole.objects.all()
        admin_list = sergy.AdminRoleSerializer(adminrole, many=True)
        return JsonResponse({"code": 200, "msg": "查询成功", "data": admin_list.data})
    def post(self, request):
        admin = models.Admin.objects.create(name=request.data.get('name'),password=request.data.get('password'))
        return JsonResponse({"code": 200, "msg": "添加成功"})



class LoginView(APIView):
  def post(self, request):
      user=request.data.get('user')
      password=request.data.get('password')
      if user and password:
        admin=models.Admin.objects.filter(name=user,password=password).first()
        if admin:
            return JsonResponse({"code":200,"msg":"登录成功"})
        else:
            return JsonResponse({"code": 400, "msg": "用户名或密码错误"})


class RoleList(APIView):
    def get(self, request):
        role = models.Role.objects.all()
        role_list = sergy.RoleSerializer(role, many=True)
        return JsonResponse({"code": 200, "msg": "查询成功", "data": role_list.data})
    def post(self, request):
        role = models.Role.objects.create(name=request.data.get('name'))
        return JsonResponse({"code": 200, "msg": "添加成功", "data": sergy.RoleSerializer(role).data})


class RoleDel(APIView):
    def post(self, request):
        models.Role.objects.filter(id=request.data.get('id')).delete()
        return JsonResponse({"code": 200, "msg": "删除成功"})

class RoleUpdate(APIView):
    def post(self, request):
        models.Role.objects.filter(id=request.data.get('id')).update(name=request.data.get('name'))
        return JsonResponse({"code": 200, "msg": "更新成功"})
class ResourceList(APIView):
    def get(self, request):
        resource = models.Resource.objects.all()
        resource_list = sergy.ResourceSerializer(resource, many=True)
        return JsonResponse({"code": 200, "msg": "查询成功", "data": resource_list.data})
    def post(self, request):
        resource = models.Resource.objects.create(name=request.data.get('name'),pid=request.data.get('pid'),url=request.data.get('url'))
        return JsonResponse({"code": 200, "msg": "添加成功", "data": sergy.ResourceSerializer(resource).data})
class ResourceDel(APIView):
    def post(self, request):
        models.Resource.objects.filter(id=request.data.get('id')).delete()
        return JsonResponse({"code": 200, "msg": "删除成功"})
class ResourceUpdate(APIView):
    def post(self, request):
        models.Resource.objects.filter(id=request.data.get('id')).update(name=request.data.get('name'),pid=request.data.get('pid'),url=request.data.get('url'))
        return JsonResponse({"code": 200, "msg": "更新成功"})

class RoleResourceList(APIView):
    def get(self, request):
        role_resource = models.RoleResource.objects.all()
        role_resource_list = sergy.RoleResourceSerializer(role_resource, many=True)
        return JsonResponse({"code": 200, "msg": "查询成功", "data": role_resource_list.data})
    def post(self, request):
        role_resource = models.RoleResource.objects.create(role=request.data.get('roleid'),resource=request.data.get('resourceid'))
        return JsonResponse({"code": 200, "msg": "添加成功", "data": sergy.RoleResourceSerializer(role_resource).data})

class RoleResourceDel(APIView):
    def post(self, request):
        models.RoleResource.objects.filter(id=request.data.get('id')).delete()
        return JsonResponse({"code": 200, "msg": "删除成功"})
class RoleResourceUpdate(APIView):
    def post(self, request):
        models.RoleResource.objects.filter(id=request.data.get('id')).update(role=request.data.get('roleid'),resource=request.data.get('resourceid'))
        return JsonResponse({"code": 200, "msg": "更新成功"})
























class TestWebsocket(APIView):
    def get(self, request):
        # 获取信息
        # 查询数据库对比

        # update = AllDataConsumersUpdate()
        # update.all_active_visit_data()
        channel_layer = get_channel_layer()
        send_data = {"error": "1号大棚温度太高", 'namelist': ['1001', '1002'],
                     'valuelist': [random.randint(10, 100), random.randint(100, 200)]}
        # send_data = {"name":rand.randint(1,100)}
        async_to_sync(channel_layer.group_send)(
            '1',  # 房间组名
            {
                'type': 'send_to_chrome',  # 消费者中处理的函数
                'data': send_data
            }
        )
        return JsonResponse({"code": 200, "msg": "更新数据成功"})

